package user_ser

import (
	"giv-vue-blog/global"
	"giv-vue-blog/models"
	"giv-vue-blog/models/ctype"
	"giv-vue-blog/utils"
	"giv-vue-blog/utils/pwd"
)

const Avatar = "/uploads/avatar/default.png"

func (UserService) CreateUser(userName, nickName, password, tel, ip string, role ctype.Role) (err error) {
	//判断用户是否存在
	var userModel models.UserModel
	count := global.DB.Where("tel =?", tel).First(&userModel).RowsAffected
	if count != 0 {
		global.Log.Error("手机号已存在")
		return err
	}
	hashPwd := pwd.HashAndSalt(password)
	addr := utils.GetAddr(ip)
	err = global.DB.Create(&models.UserModel{
		NickName:   nickName,
		UserName:   userName,
		Password:   hashPwd,
		Avatar:     Avatar,
		Tel:        tel,
		Addr:       addr,
		IP:         ip,
		Role:       role,
		SignStatus: ctype.SginEmail,
	}).Error
	if err != nil {
		global.Log.Error("用户创建失败")
		return err
	}
	return nil
}
